<?php

include_once ( "AbstractDAO.php" );

class CollectionDAO extends  AbstractDAO 
{
	//Shop Collection Part
	function GetCollectionShopByUid($uid, $count='20')
	{
		$sql="select * from shop_info si, user_shop_collection usc 
		where usc.user_id=$uid and usc.shop_id=si.id order by usc.collect_date desc limit 0, $count";
		return $this->db->GetPlan($sql);
	}
	
	function GetCollectionShopBySid($uid, $sid)
	{
		$result=$this->db->GetRow("select * from user_shop_collection where user_id = $uid and shop_id = $sid");
		if($result==false)
			return false;
		else
		{
			if($result['tags']==NULL)
				$result['tags']='';
			return $result;
		}
	}
	
	function CollectShop($uid,$sid,$tags)
	{	
		//$tagArr = explode(" ", $tags);
		$tagArr = split(" +",$tags);	
		$tags = implode(" ", array_unique($tagArr));
		if($this->GetCollectionShopBySid($uid,$sid)==false)
		{				
			$sql = "insert into user_shop_collection(user_id, shop_id, collect_date, tags) values( $uid, $sid, CURRENT_TIMESTAMP, '$tags')";
			$this->db->Insert( $sql );		
			return true;
		}
		else
		{	
			$sql = "update user_shop_collection set tags ='$tags' where user_id =$uid and shop_id =$sid";
			$this->db->Update( $sql );
			return false;
		}
	}
	
	function DeleteShop($uid,$sid)
	{	
		if($this->GetCollectionShopBySid($uid,$sid)!=false)
		{	
			$sql = "delete from user_shop_collection where user_id = $uid and shop_id = $sid";	
			$this->db->Update( $sql );		
		}
	}
	
	function GetShopCountByTag($uid, $tag='')
	{
		$sql="select count(*) from shop_info si, user_shop_collection usc where si.status=1 and usc.user_id=$uid 
		and usc.shop_id=si.id";
		if($tag!='')
			$sql=$sql." and (usc.tags='$tag' or usc.tags like '$tag %' or usc.tags like '% $tag' or usc.tags like '% $tag %')";
		return $this->db->GetValue($sql);
	}
	
	function GetShopByTag($uid, $tag='', $cpage='1',$perpage='20')
	{
		$sql="select si.id, si.plat_id, si.shop_name, si.logo_url, si.location, si.score,si.reality, si.service, si.velocity, si.rate, usc.collect_date
		from shop_info si, user_shop_collection usc where si.status=1 and usc.user_id=$uid and usc.shop_id=si.id";
		if($tag!='')
			$sql=$sql." and (usc.tags='$tag' or usc.tags like '$tag %' or usc.tags like '% $tag' or usc.tags like '% $tag %')";
		return $this->getRows($sql, $perpage, $cpage);
	}
	
	function GetShopCountByCid($uid, $cid='-1')
	{
		$sql="select count(*) from shop_info si, user_shop_collection usc where si.status=1 and usc.user_id=$uid 
		and usc.shop_id=si.id";
		if($cid!='-1')
			$sql=$sql." and (si.cate_id_list='$cid' or cate_id_list like '$cid,%' or cate_id_list like '%,$cid' or cate_id_list like '%,$cid,%')";
		return $this->db->GetValue($sql);
	}
	
	function GetShopByCid($uid, $cid='-1', $cpage='1',$perpage='20')
	{
		$sql="select si.id, si.plat_id, si.shop_name, si.logo_url, si.location, si.score, si.reality, si.service, si.velocity, si.rate,usc.collect_date 
		from shop_info si, user_shop_collection usc where si.status=1 and usc.user_id=$uid and usc.shop_id=si.id";
		if($cid!='-1')
			$sql=$sql." and (si.cate_id_list='$cid' or cate_id_list like '$cid,%' or cate_id_list like '%,$cid' or cate_id_list like '%,$cid,%')";
		return $this->getRows($sql, $perpage, $cpage);
	}
	
	/*
	 * @uid: user id
	 * @sort: 1 need sort, 0 do not
	 * @count: return count
	 */
	function GetShopTagsByUid($uid, $sort='0', $count='0')
	{
		$sql="select tags from user_shop_collection where user_id = $uid and tags!=''";
		$tagStrList=$this->db->GetPlan($sql);
		if(!$tagStrList)
			return false;
		
		//Split tag str
		$favTag1=array();
		foreach($tagStrList as $tagstr)
		{
			$tempArr=explode(" ", $tagstr['tags']);
			$favTag1=array_merge($favTag1,$tempArr);
		}
		
		$favTag2=array();
		if($sort=='0')
		{
			//Do not need sort, just delete repeated tag
			$favTag2=array_flip(array_flip($favTag1));
			if($count=='0')
				return array_slice($favTag2,0, count($favTag2));
			else
				return array_slice($favTag2, 0, $count);
		}
		else
		{
			//Need sort by frequence
			$countArr=array();
			foreach($favTag1 as $index=>$tag)
			{
				if(!isset($countArr["$tag"]))
					$countArr["$tag"]=1;
				else
					$countArr["$tag"]++;
			}
			arsort($countArr, SORT_NUMERIC);
			
			$index=0;
			foreach($countArr as $tag=>$num)
			{
				$favTag2[$index]=$tag;
				$index++;
				if($count>0&&($index==$count))
					return $favTag2;
			}
			return $favTag2;
		}
	}
	
	function GetShopCateByUid($uid, $count='0')
	{
		$sql="select si.cate_id_list from shop_info si, user_shop_collection usc where 
		usc.user_id=$uid and usc.shop_id=si.id and si.status=1";
		$cateStrList=$this->db->GetCol($sql);
		if(!$cateStrList)
			return false;
		$cateStr=$cateStrList[0];
		for($i=1;$i<count($cateStrList);$i++)
			$cateStr=$cateStr.','.$cateStrList[$i];
		$sql="select distinct id, cate_name from shop_category where id in ($cateStr)";
		if($count>'0')
			$sql=$sql." limit 0, $count";
		return $this->db->GetPlan($sql);
	}
	
	
	
	//Product Collection Part
	function GetCollectionProductByPid($uid, $pid)
	{
		$result=$this->db->GetRow("select * from user_product_collection where user_id = $uid and product_id = $pid");
		if($result==false)
			return false;
		else
		{
			if($result['tags']==NULL)
				$result['tags']='';
			return $result;
		}
	}
	
	function CollectProduct($uid,$pid,$tags)
	{
		$tagArr = split(" +",$tags);		
		$tags=implode(" ", array_unique($tagArr));	
		if($this->GetCollectionProductByPid($uid,$pid)==false)
		{	
			$sql = "insert into user_product_collection(user_id, product_id, collect_date, tags) values( $uid, $pid, CURRENT_TIMESTAMP, '$tags')";
			$this->db->Insert($sql);		
			return true;
		}
		else
		{		
			$sql = "update user_product_collection set tags ='$tags' where user_id =$uid and product_id =$pid";
			$this->db->Update( $sql );
			return false;
		}
	}
		
	function DeleteProduct($uid,$pid)
	{		
		if($this->GetCollectionProductByPid($uid,$pid)!=false)
		{		
			$sql = "delete from user_product_collection where user_id = $uid and product_id = $pid";
			$this->db->Update( $sql );				
		}
	}
	
	function GetProductCountByTag($uid, $tag='')
	{
		$sql="select count(*) from product_info pi, user_product_collection upc where pi.status=1 and upc.user_id=$uid and 
		upc.product_id=pi.id";
		if($tag!='')
			$sql=$sql." and (upc.tags='$tag' or upc.tags like '$tag %' or upc.tags like '% $tag' or upc.tags like '% $tag %')";
		return $this->db->GetValue($sql); 
	}
	
	function GetProductByTag($uid, $tag='', $cpage='1',$perpage='20')
	{
		$sql="select pi.id, pi.product_name, pi.shop_id, si.shop_name, pi.pic_path, pi.location, pi.price, pi.score, pi.reality, pi.service, pi.velocity, pi.rate, upc.collect_date 
		from shop_info si, product_info pi, user_product_collection upc where pi.status=1 and upc.user_id=$uid and 
		upc.product_id=pi.id and pi.shop_id=si.id";
		if($tag!='')
			$sql=$sql." and (upc.tags='$tag' or upc.tags like '$tag %' or upc.tags like '% $tag' or upc.tags like '% $tag %')";
		return $this->getRows($sql, $perpage, $cpage);
	}
	
	function GetProductCountByCid($uid, $cid='-1')
	{
		$sql="select count(*) from product_info pi, user_product_collection upc where pi.status=1 and upc.user_id=$uid and 
		upc.product_id=pi.id";
		if($cid!='-1')
			$sql=$sql." and pi.cate_id=$cid";
		return $this->db->GetValue($sql);
	}
	
	function GetProductByCid($uid, $cid='-1', $cpage=1,$perpage=20)
	{
		$sql="select pi.id, pi.product_name, pi.shop_id, si.shop_name, pi.pic_path, pi.location, pi.price, pi.score, pi.reality, pi.service, pi.velocity, pi.rate, upc.collect_date 
		from shop_info si, product_info pi, user_product_collection upc where pi.status=1 and upc.user_id=$uid and 
		upc.product_id=pi.id and pi.shop_id=si.id";
		if($cid!='-1')
			$sql=$sql." and pi.cate_id=$cid";
		return $this->getRows($sql, $perpage, $cpage);
	}
	
	function GetProductTagsByUid($uid, $sort='0', $count='0')
	{
		$sql="select tags from user_product_collection where user_id=$uid and tags!=''";
		$tagStrList=$this->db->GetPlan($sql);
		if(!$tagStrList)
			return false;
		
		//Split tag str
		$favTag1=array();
		foreach($tagStrList as $tagstr)
		{
			$tempArr=explode(" ", $tagstr['tags']);
			$favTag1=array_merge($favTag1,$tempArr);
		}
		
		$favTag2=array();
		if($sort=='0')
		{
			//Do not need sort, just delete repeated tag
			$favTag2=array_flip(array_flip($favTag1));
			if($count=='0')
				return array_slice($favTag2,0, count($favTag2));
			else
				return array_slice($favTag2, 0, $count);
		}
		else
		{
			//Need sort by frequence
			$countArr=array();
			foreach($favTag1 as $index=>$tag)
			{
				if(!isset($countArr["$tag"]))
					$countArr["$tag"]=1;
				else
					$countArr["$tag"]++;
			}
			arsort($countArr, SORT_NUMERIC);
			
			$index=0;
			foreach($countArr as $tag=>$num)
			{
				$favTag2[$index]=$tag;
				$index++;
				if($count>0&&($index==$count))
					return $favTag2;
			}
			return $favTag2;
		}
	}
	
	function GetProductCateByUid($uid, $count='0')
	{
		$sql="select distinct pc.id, pc.cate_name from product_category pc, product_info pi, user_product_collection upc 
		where upc.user_id=$uid and upc.product_id=pi.id and pi.status=1 and pi.cate_id=pc.id";
		if($count>'0')
			$sql=$sql." limit 0, $count";
		return $this->db->GetPlan($sql);
	}
	
	//List Collection Part
	function GetCollectionListByLid($uid, $lid)
	{
		$result=$this->db->GetRow("select * from user_list_collection where user_id = $uid and list_id = $lid");
  
		if($result==false)
			return false;
		else
		{
			if($result['tags']==NULL)
				$result['tags']='';
			return $result;
		}
	}
	
	function CollectList($uid,$lid,$tags)
	{
		$tagArr = split(" +",$tags);		
		$tags=implode(" ", array_unique($tagArr));	
		if($this->GetCollectionListByLid($uid,$lid)==false)
		{	
			$sql = "insert into user_list_collection(user_id, list_id, collect_date, tags) values( $uid, $lid, CURRENT_TIMESTAMP, '$tags')";
			$this->db->Insert($sql);		
			return true;
		}
		else
		{		
			$sql = "update user_list_collection set tags ='$tags' where user_id =$uid and list_id =$lid";
			$this->db->Update( $sql );
			return false;
		}
	}
		
	function DeleteList($uid,$lid)
	{		
		if($this->GetCollectionListByLid($uid,$lid)!=false)
		{		
			$sql = "delete from user_list_collection where user_id = $uid and list_id = $lid";
			$this->db->Update( $sql );				
		}
	}
	
	function GetListCountByTag($uid, $tag='')
	{
		$sql="select count(*) from user_list_collection ulc where ulc.user_id=$uid";
		if($tag!='')
			$sql=$sql." and (ulc.tags='$tag' or ulc.tags like '$tag %' or ulc.tags like '% $tag' or ulc.tags like '% $tag %')";
		return $this->db->GetValue($sql);
	}
	
	function GetListByTag($uid, $tag='', $cpage='1',$perpage='20')
	{
		$sql="select l.id, l.user_id, ubi.nick, l.title, l.description, l.c2c_count,l.b2c_count,l.product_count,l.create_time, 
		l.pic_path, ulc.collect_date from user_basic_info ubi, list l, user_list_collection ulc where ulc.user_id=$uid and ulc.list_id=l.id and l.user_id=ubi.id";
		if($tag!='')
			$sql=$sql." and (ulc.tags='$tag' or ulc.tags like '$tag %' or ulc.tags like '% $tag' or ulc.tags like '% $tag %')";
		return $this->getRows($sql, $perpage, $cpage);
	}
	
	function GetListTagsByUid($uid, $sort='0', $count='0')
	{
		$sql="select tags from user_list_collection where user_id=$uid and tags!=''";
		$tagStrList=$this->db->GetPlan($sql);
		if(!$tagStrList)
			return false;
		
		//Split tag str
		$favTag1=array();
		foreach($tagStrList as $tagstr)
		{
			$tempArr=explode(" ", $tagstr['tags']);
			$favTag1=array_merge($favTag1,$tempArr);
		}
		
		$favTag2=array();
		if($sort=='0')
		{
			//Do not need sort, just delete repeated tag
			$favTag2=array_flip(array_flip($favTag1));
			if($count=='0')
				return array_slice($favTag2, 0, count($favTag2));
			else
				return array_slice($favTag2, 0, $count);
		}
		else
		{
			//Need sort by frequence
			$countArr=array();
			foreach($favTag1 as $index=>$tag)
			{
				if(!isset($countArr["$tag"]))
					$countArr["$tag"]=1;
				else
					$countArr["$tag"]++;
			}
			arsort($countArr, SORT_NUMERIC);
			
			$index=0;
			foreach($countArr as $tag=>$num)
			{
				$favTag2[$index]=$tag;
				$index++;
				if($count>0&&($index==$count))
					return $favTag2;
			}
			return $favTag2;
		}
	}
}
?>